package {

	
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.objects.DisplayObject3D;
	import org.PaperBase.PaperBase;

	
	public class Rondell extends DisplayObject3D{
		private var radius:Number;
		private var centerX:Number;
		private var centerZ:Number = 1000;
		private var angle:Number;
		private var anglePlus:Number = 0;
		
		private var objArray:Array = new Array();
		
		public function Rondell(anz:int) {
			for (var i:int = 0; i < anz; i++) {
					this.addObject(new Polaroid());
			}
		}
		
		public function addObject(currentObject:Polaroid):void{
			currentObject.scale = 0.5;
			//currentObject.lock();
			currentObject.parentContainer = this;
			
			objArray.push(currentObject);
			radius = (objArray.length * 1100)/(Math.PI * 2);
			this.addChild(currentObject);
			angle = (Math.PI*2) / objArray.length;
		}
		
			public function hitHandle(e:InteractiveScene3DEvent):void {
				trace("hit handle");
			}
		
		public function removeObject(currentObject:Polaroid):DisplayObject3D{
			var tmpPolaroid:Polaroid = currentObject;
			
			objArray.splice(objArray.indexOf(tmpPolaroid),1);
			this.removeChild(tmpPolaroid);
			
			angle = (Math.PI*2) / objArray.length;
			
			return tmpPolaroid;
			
		}
		
		/*public function removeObject(currentObject:DisplayObject3D):void{
			objArray.splice(objArray.indexOf(currentObject),1);
			this.removeChild(currentObject);
			angle = (Math.PI*2) / objArray.length;
		}*/
		
		public function rotateObject():void{
			var i:Number = 0;
			for each(var item:DisplayObject3D in objArray){
				item.x = Math.cos(angle * i + anglePlus) * radius - this.x;
				item.y = 0;
				item.z = Math.sin(angle * i + anglePlus) * radius + this.z;
				i++
			}
		}
		
		public function process(mouseX:Number, width:Number):void{
			centerX = width /2;
			anglePlus += ((mouseX / width - 0.5)/10);
			rotateObject();
		}
		public function getObjects():Array {
			for each (var item:Polaroid in objArray){
				item.parentContainer = null;
			}
			objArray[0].tweenMe();
			return this.objArray.splice(0,this.objArray.length);
		}
		
		public function get getRadius():int {
			return Math.floor(this.radius);
		}
		public function dumpObjects():void {
			for each (var item:Polaroid in objArray) { 
				this.removeChild(item);
			}
			this.objArray.splice(0, this.objArray.length);
		}
		
		public function enableInteractive():void {
			for each (var item:Polaroid in objArray) {
				item.enableInteractive();
			}
		}
		public function disableInteractive():void {
			for each (var item:Polaroid in objArray) {
				item.disableInteractive();
			}
		}
		public function changeRondell(count:int):void {
			this.dumpObjects();
			
			if(count < 2){
				for (var i:int = 0; i < Math.random()*15+3; i++){ 
						this.addObject(new Polaroid());
				}
			}
			else {
				this.addObject(new Polaroid());	
				this.addObject(new Polaroid());	
				this.addObject(new Polaroid());	
				this.addObject(new VideoPolaroid());	
				this.addObject(new Polaroid());	
				this.addObject(new Polaroid());	
				this.addObject(new Polaroid());
				this.addObject(new Polaroid());
				this.addObject(new Polaroid(false));
				//this.addObject(new DocumentPolaroid());
				
			}
		}
		
		
	}
}
